﻿using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Camc.Quality.Bom;
using Camc.Quality.Bop;
using Camc.Quality.Bop.TreeObject;
using Camc.Quality.Common.CommonDate;
using Camc.Quality.DataCenter.SuiteInfoBoard;
using Camc.Quality.DataDictionary;
using Camc.Quality.Materials;
using Camc.Quality.ProductionPlan;
using Camc.Quality.Quality;
using Camc.Quality.RecordTable.FormClass;
using Camc.Quality.StoreManagement;
using Camc.Quality.StoreManagement.StoragePut;
using Camc.Quality.TrepanningManager.BomAnalyse;
using Camc.Quality.TrepanningManager.BomAnalyseDto;
using Camc.Quality.TrepanningManager.CollectBom;
using Camc.Quality.TrepanningManager.CollectBomDto;
using Camc.Quality.TrepanningManager.MatingManager;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Camc.Quality.DataCenter.BoardList;
using static Camc.Quality.StoreManagement.StoragePut.StoreStockContent;

namespace Camc.Quality.DataCenter
{
    public class SuiteInfoBoardAppService : QualityAppServiceBase
    {
        private readonly IRepository<BopNode, Guid> _bopNodeRepository;
        private readonly IRepository<BopDesc, Guid> _bopDescRepository;
        private readonly IRepository<BomDesign, Guid> _bomDesignrepository;
        private readonly IRepository<BomDesignDetail, Guid> _bomDesignDetailRepository;
        private readonly IRepository<MaterialsContent, Guid> _materialsContentrepository;
        private readonly IRepository<WorkStepEntity, Guid> _workStepEntityRepository;
        private readonly IRepository<ProcessesEntity, Guid> _processesEntityRepository;
        private readonly IRepository<LoadPieceEntity, Guid> _loadPieceEntityRepository;
        private readonly IRepository<CollectBomManager, Guid> _collectBomManagerRepository;
        private readonly IRepository<StoreStock, Guid> _storeStockRepository;
        private readonly IRepository<MaterialsContent, Guid> _materialsContentRepository;
        private readonly IRepository<StoreStockContent, Guid> _storeStockContentRepsitory;
        private readonly IRepository<LoadPieceEntity, Guid> _loadPieceRepository;
        private readonly BomAnalyseAppService _bomAnalyseAppServiceRepository;
        private readonly IRepository<CollectBomManagerChild, Guid> _collectBomManagerChildRepository;
        private readonly ProductionDesignAppService _productionDesignAppServiceRepository;
        private readonly IRepository<DeliveryDocument, Guid> _deliveryDocumentRepository;
        private readonly IRepository<MakeTask, Guid> _makeTaskRepository;
        private readonly IRepository<DeliveryOutRecord, Guid> _deliveryOutRecordRepository;
        CollectBomAppService _collectBomAppServiceRepository;
        private readonly StoreFunctionsAppService _storeFunctionsAppService;
        private readonly IRepository<MaterialsNode, Guid> _materialsNodeRepository;
        private readonly IRepository<Sys_Code, Guid> _sysCodeRepository;
        MatingManagerAppService _matingManagerAppServiceRepository;
        public SuiteInfoBoardAppService(IRepository<BopNode, Guid> bopNodeRepository,
                                  IRepository<BopDesc, Guid> bopDescRepository,
                                  IRepository<BomDesign, Guid> bomDesignrepository,
                                  IRepository<BomDesignDetail, Guid> bomDesignDetailrepository,
                                  IRepository<MaterialsContent, Guid> materialsContentrepository,
                                  IRepository<WorkStepEntity, Guid> workStepEntityRepository,
                                  IRepository<ProcessesEntity, Guid> processesEntityRepository,
                                  IRepository<LoadPieceEntity, Guid> loadPieceEntityRepository,
                                  IRepository<CollectBomManager, Guid> collectBomManagerRepository,
                                  IRepository<StoreStock, Guid> storeStockRepository,
                                  IRepository<MaterialsContent, Guid> materialsContentRepository,
                                  IRepository<StoreStockContent, Guid> storeStockContentRepsitory,
                                  IRepository<LoadPieceEntity, Guid> loadPieceRepository,
                                  BomAnalyseAppService bomAnalyseAppServiceRepository,
                                  IRepository<CollectBomManagerChild, Guid> collectBomManagerChildRepository,
                                  ProductionDesignAppService productionDesignAppServiceRepository,
                                  IRepository<DeliveryDocument, Guid> deliveryDocumentRepository,
                                  IRepository<MakeTask, Guid> makeTaskRepository,
                                  IRepository<DeliveryOutRecord, Guid> deliveryOutRecordRepository,
                                  CollectBomAppService collectBomAppServiceRepository,
                                  StoreFunctionsAppService storeFunctionsAppService,
                                  IRepository<MaterialsNode, Guid> materialsNodeRepository,
                                  IRepository<Sys_Code, Guid> sysCodeRepository,
                                  MatingManagerAppService matingManagerAppServiceRepository
            )
        {
            _bopNodeRepository = bopNodeRepository;
            _bopDescRepository = bopDescRepository;
            _bomDesignDetailRepository = bomDesignDetailrepository;
            _bomDesignrepository = bomDesignrepository;
            _materialsContentrepository = materialsContentrepository;
            _workStepEntityRepository = workStepEntityRepository;
            _processesEntityRepository = processesEntityRepository;
            _loadPieceEntityRepository = loadPieceEntityRepository;
            _collectBomManagerRepository = collectBomManagerRepository;
            _storeStockRepository = storeStockRepository;
            _materialsContentRepository = materialsContentRepository;
            _storeStockContentRepsitory = storeStockContentRepsitory;
            _loadPieceRepository = loadPieceRepository;
            _bomAnalyseAppServiceRepository = bomAnalyseAppServiceRepository;
            _productionDesignAppServiceRepository = productionDesignAppServiceRepository;
            _deliveryDocumentRepository = deliveryDocumentRepository;
            _makeTaskRepository = makeTaskRepository;
            _deliveryOutRecordRepository = deliveryOutRecordRepository;
            _collectBomAppServiceRepository = collectBomAppServiceRepository;
            _storeFunctionsAppService = storeFunctionsAppService;
            _materialsNodeRepository = materialsNodeRepository;
            _sysCodeRepository = sysCodeRepository;
            _matingManagerAppServiceRepository = matingManagerAppServiceRepository;
        }
        /// <summary>
        /// 型号发次下拉列表
        /// </summary>
        /// <returns></returns>
        public List<LabelCollection> GetModelLotNoForBoard()
        {
            var query = from bd in _bopDescRepository.GetAll()
                        select new LabelCollection
                        {
                            Label = bd.Model + "-" + bd.LotNo,
                            Value = bd.Id
                        };
            if (query.Count() > 0)
            {
                return query.ToList();
            }
            return new List<LabelCollection>();
        }
        /// <summary>
        /// 芯级配套数据查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public BoardList GetNodeDropList(Guid id)
        {
            var bopdesc = _bopDescRepository.FirstOrDefault(s => s.Id == id);
            BoardList boardList = new BoardList();
            List<BoardXJList> boardXJLists = new List<BoardXJList>();
            //获取芯级信息
            var data = (from node in _bopNodeRepository.GetAll().Where(s => s.ParentId == id)
                        join desc in _bopDescRepository.GetAll()
                        on node.ParentId equals desc.Id
                        select new
                        {
                            Id = node.Id,
                            Name = node.NodeName,
                            NodeDrawingNo = node.NodeDrawingNo,
                            ModelId = (Guid)desc.ModelId,
                            LotNoId = (Guid)desc.LotNoId,
                            MaterialNo = node.NodeDrawingNo,
                            NodeCode = node.NodeCode,
                            BopDescId = node.BopDescId,
                            CreationTime = node.CreationTime

                        }).ToList();
            int u = 0;
            var query = data.OrderBy(ee =>
            {
                u = Array.IndexOf(new string[]
                {
                    "芯一级",
                    "芯二级",
                    "芯三级",
                    "星罩",
                    "助推Ⅰ",
                    "助推Ⅱ",
                    "助推Ⅲ",
                    "助推Ⅳ"
                }, ee.Name);
                if (u != -1) { return u; }
                else { return int.MaxValue; }
            });
            var loadQuery = from temp in _loadPieceRepository.GetAll()
                            select new
                            {
                                Id = temp.Id,
                                Number = temp.Number,
                                WorkStepEntityId = temp.WorkStepEntityId,
                                Sort = temp.Sort
                            };
            var stockContent = from temp in _storeStockContentRepsitory.GetAll().Where(d => d.LoadDetailsId != null)
                               select new
                               {
                                   LoadDetailsId = temp.LoadDetailsId,
                                   IsSuit = temp.IsSuit,
                                   MaterialsContentId = temp.MaterialsContentId,
                                   IsPreSuit = temp.IsPreSuit,
                                   OutboundState = temp.OutboundState
                               };
            //自由出库数量
            var delivery = (from ddr in _deliveryDocumentRepository.GetAll()
                            join mt in _makeTaskRepository.GetAll().Where(s => s.ModelId == bopdesc.ModelId && s.LotNoId == bopdesc.LotNoId)
                            on ddr.WhereAbouts equals mt.JobNumber
                            select new
                            {
                                Id = ddr.Id,
                                StockIdList = (from dd in _deliveryOutRecordRepository.GetAll().Where(d => d.DeliveryDocumentId == ddr.Id)
                                               join ssc in _storeStockContentRepsitory.GetAll().Where(s => s.RetrievalState == StoreManagement.OutboundRetrieval.新增 && s.DeliveryDocumentId == ddr.Id)
                                               on dd.StoreStockContentId equals ssc.Id
                                               select dd.StoreStockContentId).Count()
                            });
            var freeOutNum = delivery.Sum(d => d.StockIdList);
            foreach (var item in query)
            {
                BoardXJList boardXJList = new BoardXJList();
                decimal processDosage = 0;//用量
                decimal preSuiteStock = 0;//预配套数量
                decimal suiteStock = 0;//配套数量
                decimal outboundedNum = 0;//配套出库数量
                var techQuery = _bopNodeRepository.GetAll().Where(d => d.NodeCode.StartsWith(item.NodeCode) && d.BopDescId == item.BopDescId).Where(d => d.TechniqueEntityId != null).Select(d => d.TechniqueEntityId).ToList();
                if (techQuery.Count > 0)
                {
                    var collectManager = _collectBomManagerRepository.FirstOrDefaultAsync(t => t.XJName == item.Name && t.XJDrawingNo == item.NodeDrawingNo && t.ModuleId == item.ModelId && t.SendTimeId == item.LotNoId).Result;
                    if (collectManager != null)
                    {
                        List<CollectBomMaterialsContentDto> collect = new List<CollectBomMaterialsContentDto>();
                        collect = _collectBomAppServiceRepository.GetCollectBomMaterialsContent(collectManager.XJId);
                        if (collect != null) processDosage = collect.Sum(d => d.Unit1BomDosageXj);
                    }
                    var workIds = _workStepEntityRepository.GetAll().Where(d => techQuery.Contains(d.TechniqueEntityId)).Select(d => d.Id);
                    var loadList = from cc in loadQuery
                                   join mm in workIds on cc.WorkStepEntityId equals mm
                                   select cc;
                    //processDosage = loadList.Sum(d => d.Number);
                    var loadIds = loadList.Select(d => d.Id);
                    //suiteStock = stockContent.Count(d => loadIds.Contains(d.LoadDetailsId.Value) && d.IsSuit == IsSuits.是);//配套数量
                    var totalMaterial = _bomAnalyseAppServiceRepository.GetExternalCall(item.Name, item.NodeDrawingNo, item.ModelId, item.LotNoId);
                    suiteStock = totalMaterial.Sum(d => d.SuiteStockAll);
                    preSuiteStock = totalMaterial.Sum(d => d.PreSuiteStockAll);
                    outboundedNum = stockContent.Count(d => loadIds.Contains(d.LoadDetailsId.Value) && d.OutboundState == OutboundStates.出库);//已出库
                }
                boardXJList.Id = item.Id;
                boardXJList.Name = item.Name;
                boardXJList.ModelId = item.ModelId;
                boardXJList.LotNoId = item.LotNoId;
                boardXJList.MaterialNo = item.MaterialNo;
                boardXJList.Quantity = (decimal)processDosage;
                boardXJList.PreSuiteQuantity = (decimal)preSuiteStock;
                boardXJList.SuiteQuantity = (decimal)suiteStock;
                boardXJList.OutboundQuantity = (decimal)outboundedNum;
                if (processDosage != 0)
                {
                    boardXJList.PreSuitePercent = decimal.Round((preSuiteStock * 100) / processDosage);
                    boardXJList.SuitePercent = decimal.Round((suiteStock * 100) / processDosage);
                    boardXJList.OutboundPercent = decimal.Round((outboundedNum * 100) / processDosage);
                }
                else
                {
                    boardXJList.PreSuitePercent = 0;
                    boardXJList.SuitePercent = 0;
                    boardXJList.OutboundPercent = 0;
                }
                boardXJLists.Add(boardXJList);
            }
            boardList.Id = id;
            boardList.Quantity = boardXJLists.Sum(s => s.Quantity);
            boardList.PreSuiteQuantity = boardXJLists.Sum(s => s.PreSuiteQuantity);
            boardList.SuiteQuantity = boardXJLists.Sum(s => s.SuiteQuantity);
            boardList.OutboundQuantity = boardXJLists.Sum(s => s.OutboundQuantity) + freeOutNum;
            if (boardList.Quantity != 0)
            {
                boardList.PreSuitePercent = decimal.Round((boardList.PreSuiteQuantity * 100) / boardList.Quantity);
                boardList.SuitePercent = decimal.Round((boardList.SuiteQuantity * 100) / boardList.Quantity);
                boardList.OutboundPercent = decimal.Round((boardList.OutboundQuantity * 100) / boardList.Quantity);
            }
            else
            {
                boardList.PreSuitePercent = 0;
                boardList.SuitePercent = 0;
                boardList.OutboundPercent = 0;
            }
            boardList.BoardXJLists = boardXJLists;
            return boardList;
        }
        /// <summary>
        /// 预配套查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Task<PagedResultDto<BomAnalyseContentDto>> GetPerSuiteInfo(Guid id)
        {
            List<BomAnalyseContentDto> bomAnalyseContentDtos = new List<BomAnalyseContentDto>();
            if (id == Guid.Empty) return null;


            var node = _bopNodeRepository.FirstOrDefault(s => s.Id == id);
            var desc = _bopDescRepository.FirstOrDefault(s => s.Id == node.ParentId);
            var xj = _collectBomManagerRepository.FirstOrDefault(s => s.XJName == node.NodeName && s.XJDrawingNo == node.NodeDrawingNo && s.ModuleId == desc.ModelId && s.SendTimeId == desc.LotNoId);
            if (xj != null)
            {
                BomAnalyseManagerEditDto bomAnalyseManagerEditDto = new BomAnalyseManagerEditDto();
                bomAnalyseManagerEditDto.Module = desc.Model;
                bomAnalyseManagerEditDto.ModuleId = (Guid)desc.ModelId;
                bomAnalyseManagerEditDto.SendTime = desc.LotNo;
                bomAnalyseManagerEditDto.SendTimeId = (Guid)desc.LotNoId;
                bomAnalyseManagerEditDto.XJName = node.NodeName;
                bomAnalyseManagerEditDto.XJId = xj.XJId;
                bomAnalyseManagerEditDto.MaxResultCount = 20000;
                bomAnalyseManagerEditDto.SkipCount = 0;
                bomAnalyseManagerEditDto.Sorting = "CreationTime Desc";
                var makeTask = _makeTaskRepository.GetAll().Where(d => d.ModelId == bomAnalyseManagerEditDto.ModuleId && d.LotNoId == bomAnalyseManagerEditDto.SendTimeId).FirstOrDefault();
                if (makeTask != null)
                {
                    bomAnalyseManagerEditDto.StoreId = makeTask.StoreId;
                }
                var bomAnalyseContentDto = _bomAnalyseAppServiceRepository.GetBomAalyseContent(bomAnalyseManagerEditDto);
                return bomAnalyseContentDto;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 根据芯级获取树结构
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AssignmentTreeOutput GetProductionDesigenTree(Guid id)
        {
            var node = _bopNodeRepository.FirstOrDefault(s => s.Id == id);
            var desc = _bopDescRepository.FirstOrDefault(s => s.Id == node.ParentId);
            var assignmentTreeOutput = _productionDesignAppServiceRepository.GetProductionDesigenTree(desc.ModelId, desc.LotNoId);
            return assignmentTreeOutput;
        }
        /// <summary>
        /// 配套显示
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public MatingTreeResult DisplayMatingNodeDetailsForBoard(PartsOutboundInput input)
        {
            MatingTreeResult result = new MatingTreeResult();
            if (input.NodeId != Guid.Empty)
            {
                int route = _matingManagerAppServiceRepository.GetRouteType(input.NodeId);
                List<MatingTreeGridDto> outputs = new List<MatingTreeGridDto>();
                var loadQuery = _loadPieceRepository.GetAll();
                var stockContent = _storeStockContentRepsitory.GetAll();
                int Level = 0;
                switch (route)
                {
                    case 1:
                    case 2:
                        var bopNodes = _bopNodeRepository.GetAll().Where(d => d.ParentId == input.NodeId).OrderBy(d => d.CreationTime).ToList();
                        if (bopNodes.Count() > 0)
                        {
                            foreach (var item in bopNodes)
                            {
                                int ProcessDosage = 0;
                                int SuiteStock = 0;
                                int OutboundedNum = 0;
                                int OutboundQuantity = 0;
                                int ShortageNum = 0;
                                string AssortState = "";
                                string matingName = "";
                                List<MatingDetaillist> matingDetaillists = new List<MatingDetaillist>();
                                //获取该部套下的工艺规程id
                                var techQuery = _bopNodeRepository.GetAll().Where(d => d.NodeCode.StartsWith(item.NodeCode) && d.BopDescId == item.BopDescId).Where(d => d.TechniqueEntityId != null).Select(d => d.TechniqueEntityId).ToList();
                                if (techQuery.Count > 0)
                                {
                                    var moduleLotNo = _bopDescRepository.FirstOrDefaultAsync(t => t.Id == item.BopDescId).Result;
                                    //根据工艺规程id获取部套下所有的工步
                                    var workIds = _workStepEntityRepository.GetAll().Where(d => techQuery.Contains(d.TechniqueEntityId)).Select(d => d.Id);
                                    //获取工步下所有装入件
                                    var loadData = from loadPieceList in _loadPieceRepository.GetAll().Where(d => workIds.Contains(d.WorkStepEntityId)).OrderBy(d => d.Sort)
                                                   join materialsList in _materialsContentRepository.GetAll() on loadPieceList.MaterialsContentId equals materialsList.Id
                                                   select loadPieceList;
                                    CollectBomManager collectBomManager = new CollectBomManager();
                                    BopNode bop = new BopNode();
                                    ProcessDosage = (from loadPieceList in loadData.Where(d => workIds.Contains(d.WorkStepEntityId)) select loadPieceList).Sum(t => t.Number);

                                    if (item.ParentCode == "root")
                                    {
                                        collectBomManager = _collectBomManagerRepository.FirstOrDefaultAsync(t => t.XJName == item.NodeName && t.XJDrawingNo == item.NodeDrawingNo && t.ModuleId == moduleLotNo.ModelId && t.SendTimeId == moduleLotNo.LotNoId).Result;
                                        bop = _bopNodeRepository.GetAll().Where(d => d.Id == item.Id).FirstOrDefault();
                                    }
                                    else
                                    {
                                        bop = _bopNodeRepository.GetAll().Where(d => d.NodeCode == item.ParentCode.Substring(0, 3) && d.BopDescId == item.BopDescId).FirstOrDefault();

                                        collectBomManager = _collectBomManagerRepository.FirstOrDefaultAsync(t => t.XJName == bop.NodeName && t.XJDrawingNo == bop.NodeDrawingNo && t.ModuleId == moduleLotNo.ModelId && t.SendTimeId == moduleLotNo.LotNoId).Result;
                                    }
                                    //找出该芯级下所有的规程id
                                    var techQueryThwo = _bopNodeRepository.GetAll().Where(d => d.ParentCode.StartsWith(bop.NodeCode) && d.BopDescId == bop.BopDescId).Where(d => d.TechniqueEntityId != null).Select(d => d.TechniqueEntityId).ToList();
                                    //根据规程id获取所有工步
                                    var workIdsTwoAll = _workStepEntityRepository.GetAll().Where(d => techQueryThwo.Contains(d.TechniqueEntityId)).Select(d => d.Id);
                                    //找出该芯级下所有的装入件
                                    var loadListTwo = (from cc in loadQuery
                                                       join mm in workIdsTwoAll on cc.WorkStepEntityId equals mm
                                                       select cc).AsNoTracking();
                                    if (collectBomManager == null)
                                    {
                                        result.Message = "该芯级下没有汇总数据，请检查";
                                        result.code = 1;
                                        return result;
                                    }
                                    var collectBomMaterialsContentDtos = _collectBomAppServiceRepository.GetCollectBomMaterialsContent(collectBomManager.XJId);
                                    if (collectBomMaterialsContentDtos == null)
                                    {
                                        result.Message = "该芯级下没有汇总数据，请检查";
                                        result.code = 1;
                                        return result;
                                    }
                                    //找到这个芯级下所有的库存信息 汇总 storeStock主表的id 用于限制芯级数据
                                    var storeStockContentFirst = _storeStockContentRepsitory.GetAll().Where(t => t.BomAnalysecontentId == collectBomManager.Id && t.ProductCodeId.Contains(collectBomManager.ModuleId.ToString()) && t.SendTimeId.Contains(collectBomManager.SendTimeId.ToString()));
                                    var storeStockId = storeStockContentFirst.Select(t => t.StoreStockId).Distinct();
                                    //查询芯级下的所有预配套数据
                                    var queryCollectMatingNumberAlys = from queryloadContent in collectBomMaterialsContentDtos.AsQueryable()
                                                                       join queryStoreStocks in _storeStockRepository.GetAll() on
                                                                     queryloadContent.MerialsId equals queryStoreStocks.MaterialsContentId
                                                                       join queryStoreStockContents in stockContent.Where(t => t.Batch == "01" && t.IsPreSuit == IsSuits.是 && t.BomAnalysecontentId != null && t.BomAnalysecontentId == collectBomManager.Id) on queryStoreStocks.Id equals queryStoreStockContents.StoreStockId
                                                                       select queryStoreStockContents;
                                    //查询该部套下的所有配套数据
                                    var queryCollectMatingNumber = from queryloadContent in loadData
                                                                   join queryStoreStocks in _storeStockRepository.GetAll() on
                                                                     queryloadContent.MaterialsContentId equals queryStoreStocks.MaterialsContentId
                                                                   join queryStoreStockContents in stockContent.Where(t => t.Batch == "01" && t.IsPreSuit == IsSuits.是 && t.BomAnalysecontentId != null && t.IsSuit == IsSuits.是 && t.LoadDetailsId != null && t.WorkStepId != null && t.BomAnalysecontentId == collectBomManager.Id) on queryStoreStocks.Id equals queryStoreStockContents.StoreStockId
                                                                   where queryStoreStockContents.LoadDetailsId == queryloadContent.Id
                                                                   select queryStoreStockContents;

                                    //计算芯级可用预配套数量 汇总芯级配套数据
                                    var queryyptListCount = from queryloadContent in loadListTwo
                                                            join queryStoreStocks in _storeStockRepository.GetAll() on
                                                              queryloadContent.MaterialsContentId equals queryStoreStocks.MaterialsContentId
                                                            join queryStoreStockContents in stockContent.Where(t => t.Batch == "01" && t.IsPreSuit == IsSuits.是 && t.BomAnalysecontentId != null && t.IsSuit == IsSuits.是 && t.LoadDetailsId != null && t.WorkStepId != null && t.BomAnalysecontentId == collectBomManager.Id) on queryStoreStocks.Id equals queryStoreStockContents.StoreStockId
                                                            where queryStoreStockContents.LoadDetailsId == queryloadContent.Id
                                                            select new StoreStockContent
                                                            {
                                                                ProductDrawingNo = queryStoreStockContents.ProductDrawingNo,
                                                                Module = queryStoreStockContents.Module,
                                                                WorkStepId = queryloadContent.WorkStepEntityId,
                                                                StoreStockId = queryStoreStocks.Id,
                                                                MaterialsContentId = queryStoreStocks.MaterialsContentId,
                                                                BomAnalysecontentId = queryStoreStockContents.BomAnalysecontentId
                                                            };
                                    //&& queryCollectMatingNumber != null && queryCollectMatingNumber.Count() > 0
                                    if (queryyptListCount != null && queryyptListCount.Count() > 0)
                                    {
                                        matingDetaillists = (from tempLoad in loadData
                                                             join tempStroeStocks in _storeStockRepository.GetAll()
                                                             on tempLoad.MaterialsContentId equals tempStroeStocks.MaterialsContentId
                                                             join storestocks in storeStockId on tempStroeStocks.Id equals storestocks
                                                             //join tempStockContent in _storeStockContentRepsitory.GetAll() on tempStroeStocks.Id equals tempStockContent.StoreStockId
                                                             join tempMaterial in _materialsContentRepository.GetAll() on tempStroeStocks.MaterialsContentId equals tempMaterial.Id
                                                                 into tm
                                                             from tmi in tm.DefaultIfEmpty()
                                                                 //join queryyptlistCount in queryyptListCount on storestocks == null?Guid.Empty: storestocks equals queryyptlistCount ==null?Guid.Empty: queryyptlistCount.StoreStockId
                                                                 //into qlc from ql in qlc.DefaultIfEmpty()
                                                             join tempNode in _materialsNodeRepository.GetAll() on tmi.MaterialsNodeId equals tempNode.Id
                                                             into tn
                                                             from tpn in tn.DefaultIfEmpty()
                                                                 //join tempSearch in queryyptList on tempLoad ==null? Guid.Empty: tempLoad.MaterialsContentId equals (tempSearch == null ? Guid.Empty : tempSearch.MaterialsContentId) into ca
                                                                 //from ts in ca.DefaultIfEmpty()
                                                             join tempSysCode in _sysCodeRepository.GetAll() on tmi.UnitId equals tempSysCode.Id
                                                                into sc
                                                             from cc in sc.DefaultIfEmpty()
                                                                 //where (ts == null ? Guid.Empty : ts.LoadDetailsId) == tempLoad.Id
                                                             orderby tempLoad.Sort
                                                             //where (ts==null?Guid.Empty:ts.LoadDetailsId) == tempLoad.Id
                                                             select new MatingDetaillist
                                                             {
                                                                 data = new MatingEditChildData()
                                                                 {
                                                                     Code = tmi == null ? "" : tmi.MaterialNo,//物料代码
                                                                     Module = tmi == null ? "" : tmi.Model,//规格
                                                                     StoreStockId = tempStroeStocks.Id,
                                                                     CollectId = collectBomManager.Id,
                                                                     Number = tempLoad.Number,//工艺用量
                                                                     PreSuiteStock = queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectBomManager.Id),
                                                                     DeliveryStock = queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id && t.OutboundState == OutboundStates.出库),
                                                                     UsableMatingQuantityStock = queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectBomManager.Id) - queryyptListCount.Count(t => t.StoreStockId == tempStroeStocks.Id),
                                                                     //queryyptListCount.FirstOrDefault(t => t.StoreStockId == storestocks).Quantity)
                                                                     WorkStepId = tempLoad.WorkStepEntityId,
                                                                     SuiteStock = queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id),
                                                                     LoadDetailsId = tempLoad.Id,
                                                                     // 
                                                                     //tempStroeStocks.PreSuiteStock - tempStroeStocks.SuiteStock != 0 说明存在可用的配套数量 有可能存在负数
                                                                     //- ts.Quantity
                                                                     //&& tempLoad.Number - (tempStroeStocks.PreSuiteStock - tempStroeStocks.SuiteStock)>0
                                                                     StarvingStock = tempLoad.Number - (queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectBomManager.Id) - queryyptListCount.Count(t => t.StoreStockId == tempStroeStocks.Id)) - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) <= 0 ? 0 :
                                                         tempLoad.Number - (queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectBomManager.Id) - queryyptListCount.Count(t => t.StoreStockId == tempStroeStocks.Id)) - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id),
                                                                     MatingStatus = tempLoad.Number - (queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id) - queryyptListCount.Count(t => t.StoreStockId == tempStroeStocks.Id)) - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) > 0 ? (int)AssortStatus.缺料 :
                                                         tempLoad.Number - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) <= (queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectBomManager.Id)) - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) && queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) != 0 && tempLoad.Number > queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) ? (int)AssortStatus.配套中 : tempLoad.Number == queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) ? (int)AssortStatus.已配套 : (int)AssortStatus.未配套,
                                                                     MatingStatusName = tempLoad.Number - (queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id) - queryyptListCount.Count(t => t.StoreStockId == tempStroeStocks.Id)) - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) > 0 ? AssortStatus.缺料.ToString() :
                                                         tempLoad.Number - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) <= (queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectBomManager.Id)) - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) && queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) != 0 && tempLoad.Number > queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) ? AssortStatus.配套中.ToString() : tempLoad.Number == queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) ? AssortStatus.已配套.ToString() : "未配套",
                                                                     CompletePeople = tempStroeStocks.CompletePeople,
                                                                     CompleteTime = tempStroeStocks.CompleteTime.ToString(),
                                                                     PreSuiteName = tempStroeStocks.PreSuiteName,
                                                                     PreSuiteTime = tempStroeStocks.PreSuiteTime.ToString(),
                                                                     ProductNumber = tempStroeStocks.ProductNumber
                                                                 }
                                                             }).ToList();

                                        if (matingDetaillists != null && matingDetaillists.Count > 0)
                                        {
                                            var tempStore = (from tqy in loadData
                                                             join queryLoadData in matingDetaillists on tqy.Id equals queryLoadData.data.LoadDetailsId
                                                             into qld
                                                             from qlda in qld.DefaultIfEmpty()
                                                             select new MatingDetaillist
                                                             {
                                                                 data = new MatingEditChildData
                                                                 {
                                                                     HJXH = qlda == null ? "" : qlda.data.HJXH,
                                                                     HJFC = qlda == null ? "" : qlda.data.HJFC,
                                                                     CollectId = qlda == null ? Guid.Empty : qlda.data.CollectId,
                                                                     Code = qlda == null ? tqy.Code : qlda.data.Code,
                                                                     CompletePeople = qlda == null ? "" : qlda.data.CompletePeople,
                                                                     CompleteTime = qlda == null ? "" : qlda.data.CompleteTime,
                                                                     DeliveryStock = qlda == null ? 0 : qlda.data.DeliveryStock,
                                                                     Id = qlda == null ? Guid.Empty : qlda.data.Id,
                                                                     IsSuit = qlda == null ? 0 : qlda.data.IsSuit,
                                                                     JoinText = qlda == null ? "" : qlda.data.JoinText,
                                                                     LevelName = qlda == null ? "" : qlda.data.LevelName,
                                                                     LevelNum = qlda == null ? 0 : qlda.data.LevelNum,
                                                                     LoadDetailsId = qlda == null ? tqy.Id : qlda.data.LoadDetailsId,
                                                                     MaterialsNodeName = qlda == null ? "" : qlda.data.MaterialsNodeName,
                                                                     MaterialsTypeName = qlda == null ? "" : qlda.data.MaterialsTypeName,
                                                                     MatingStatus = qlda == null ? (int)AssortStatus.缺料 : qlda.data.MatingStatus,
                                                                     MatingStatusName = qlda == null ? "缺料" : qlda.data.MatingStatusName,
                                                                     Module = qlda == null ? "" : qlda.data.Module,
                                                                     Name = tqy.Name,
                                                                     NodeLevel = qlda == null ? 10 : qlda.data.NodeLevel,
                                                                     Number = qlda == null ? tqy.Number : qlda.data.Number,
                                                                     PreSuiteName = qlda == null ? "" : qlda.data.PreSuiteName,
                                                                     PreSuiteStock = qlda == null ? 0 : qlda.data.PreSuiteStock,
                                                                     PreSuiteTime = qlda == null ? "" : qlda.data.PreSuiteTime,
                                                                     ProductNumber = qlda == null ? "" : qlda.data.ProductNumber,
                                                                     StarvingStock = qlda == null ? 0 : qlda.data.StarvingStock,
                                                                     StoreStockId = qlda == null ? Guid.Empty : qlda.data.StoreStockId,
                                                                     SuiteStock = qlda == null ? 0 : qlda.data.SuiteStock,
                                                                     Unit = qlda == null ? "" : qlda.data.Unit,
                                                                     UsableMatingQuantityStock = qlda == null ? 0 : qlda.data.UsableMatingQuantityStock,
                                                                     UsableQuantityStock = qlda == null ? 0 : qlda.data.UsableQuantityStock,
                                                                     WorkStepId = qlda == null ? tqy.WorkStepEntityId : qlda.data.WorkStepId,
                                                                     XJName = qlda == null ? "" : qlda.data.XJName,
                                                                 }
                                                             }
                                                           ).ToList();
                                            foreach (MatingDetaillist matingDetaillist in tempStore)
                                            {
                                                var bs = matingDetaillists.Where(t => t.data.MatingStatus == (int)AssortStatus.缺料);

                                                if (bs != null && bs.Count() > 0)
                                                {
                                                    matingName = AssortStatus.缺料.ToString();
                                                    break;
                                                }
                                                if (matingDetaillist.data.MatingStatus == (int)AssortStatus.缺料)
                                                {
                                                    matingName = AssortStatus.缺料.ToString();
                                                    break;
                                                }
                                                else if (matingDetaillist.data.MatingStatus == (int)AssortStatus.配套中)
                                                {
                                                    matingName = AssortStatus.配套中.ToString();
                                                    break;
                                                }
                                                else if (matingDetaillist.data.MatingStatus == (int)AssortStatus.已配套)
                                                {
                                                    matingName = AssortStatus.已配套.ToString();
                                                    break;
                                                }
                                                else if (matingDetaillist.data.MatingStatus == (int)AssortStatus.未配套)
                                                {
                                                    matingName = AssortStatus.未配套.ToString();
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                    MatingTreeGridDto mating = new MatingTreeGridDto();
                                    MatingEditParentData temp = new MatingEditParentData();
                                    if (matingDetaillists.Count > 0)
                                    {
                                        //计算提取芯级预配套数量、可用预配套数量 进行去重
                                        var contentRequest = matingDetaillists.Select(x => new { x.data.Code, x.data.Module, x.data.PreSuiteStock, x.data.UsableMatingQuantityStock }).Distinct();
                                        //计算配套数量缺料数量的汇总

                                        var groupByBtContent = (from guoupAll in (from groupContent in (from queryStoreStock in matingDetaillists select queryStoreStock)
                                                                                  group groupContent by new { groupContent.data.CollectId, groupContent.data.Module, groupContent.data.Code } into gc
                                                                                  select new
                                                                                  {
                                                                                      PreSuiteStockAll = contentRequest.Where(t => t.Module == gc.Key.Module && t.Code == gc.Key.Code).Sum(t => t.PreSuiteStock),
                                                                                      UsableMatingQuantityStockAll = contentRequest.Where(t => t.Module == gc.Key.Module && t.Code == gc.Key.Code).Sum(t => t.UsableMatingQuantityStock),//可用预配套数量
                                                                                      SuiteStockAll = gc.Sum(t => t.data.SuiteStock),
                                                                                      StarvingStockAll = gc.Sum(t => t.data.StarvingStock),
                                                                                      DeliveryStockAll = gc.Sum(t => t.data.DeliveryStock),
                                                                                      CollectId = gc.Key.CollectId
                                                                                  })
                                                                group guoupAll by new { guoupAll.CollectId } into all
                                                                select new
                                                                {
                                                                    PreSuiteStockAll = all.Sum(t => t.PreSuiteStockAll),
                                                                    UsableMatingQuantityStockAll = all.Sum(t => t.UsableMatingQuantityStockAll),
                                                                    SuiteStockAll = all.Sum(t => t.SuiteStockAll),
                                                                    StarvingStockAll = all.Sum(t => t.StarvingStockAll),
                                                                    DeliveryStockAll = all.Sum(t => t.DeliveryStockAll)
                                                                }).FirstOrDefault();


                                        temp.Code = item.NodeDrawingNo;
                                        temp.Name = item.NodeName;
                                        temp.SuiteStock = SuiteStock;
                                        temp.MatingStatusName = matingName;
                                        //temp.OutboundedNum = OutboundedNum;
                                        //temp.OutboundQuantity = OutboundQuantity;
                                        //temp.ShortageNum = ShortageNum;
                                        //temp.AssortState = AssortState;
                                        Level++;
                                        temp.LevelName = Level.ToString();
                                        if (ProcessDosage > (groupByBtContent.UsableMatingQuantityStockAll + groupByBtContent.SuiteStockAll))
                                        {
                                            temp.MatingStatusName = AssortStatus.缺料.ToString();
                                            temp.StarvingStock = ProcessDosage - (groupByBtContent.UsableMatingQuantityStockAll + groupByBtContent.SuiteStockAll);
                                        }
                                        else
                                        {
                                            temp.MatingStatusName = matingName;
                                            temp.StarvingStock = groupByBtContent.StarvingStockAll;
                                        }
                                        temp.PreSuiteStock = groupByBtContent.PreSuiteStockAll;
                                        temp.Number = ProcessDosage;
                                        temp.UsableMatingQuantityStock = groupByBtContent.UsableMatingQuantityStockAll;
                                        temp.SuiteStock = groupByBtContent.SuiteStockAll;
                                        temp.DeliveryStock = groupByBtContent.DeliveryStockAll;
                                        mating.data = temp;
                                        outputs.Add(mating);
                                    }
                                    else
                                    {
                                        temp.Code = item.NodeDrawingNo;
                                        temp.Name = item.NodeName;
                                        temp.SuiteStock = SuiteStock;
                                        temp.MatingStatusName = matingName;

                                        temp.MatingStatusName = AssortStatus.缺料.ToString();
                                        temp.StarvingStock = 0;
                                        temp.PreSuiteStock = 0;
                                        temp.Number = ProcessDosage;
                                        temp.UsableMatingQuantityStock = 0;
                                        temp.SuiteStock = 0;
                                        temp.DeliveryStock = 0;
                                        Level++;
                                        temp.LevelName = Level.ToString();
                                        mating.data = temp;
                                        outputs.Add(mating);
                                    }


                                }
                            }
                        }
                        break;
                    case 3:
                        int techPreSuiteStock = 0;
                        int suiteStockAll = 0;
                        var bopNode = _bopNodeRepository.Get(input.NodeId);
                        var techId = bopNode.TechniqueEntityId;
                        var proTemp = _processesEntityRepository.GetAll().Where(d => d.TechniqueEntityId == techId).OrderBy(d => d.ProcessesSequence).ToList();

                        var coreGradeNode = _bopNodeRepository.GetAll().Where(d => d.NodeCode == bopNode.ParentCode.Substring(0, 3) && d.ParentId == bopNode.BopDescId).FirstOrDefault();
                        if (coreGradeNode == null)
                        {
                            return new MatingTreeResult();
                            //throw new UserFriendlyException("未找到该节点对应的芯级!");
                            //var totalMaterial = _bomAnalyseAppServiceRepository.GetExternalCall(coreGradeNode.NodeName, coreGradeNode.NodeDrawingNo, bopDesc.ModelId, bopDesc.LotNoId);
                        }
                        var bopDesc = _bopDescRepository.Get(coreGradeNode.BopDescId);
                        //找出所有的规程  （找到所有规程 能找到所有的装入件、contains）
                        var techQueryThree = _bopNodeRepository.GetAll().Where(d => d.ParentCode.StartsWith(coreGradeNode.NodeCode) && d.BopDescId == coreGradeNode.BopDescId).Where(d => d.TechniqueEntityId != null).Select(d => d.TechniqueEntityId).ToList();
                        var workIdsThreeAll = _workStepEntityRepository.GetAll().Where(d => techQueryThree.Contains(d.TechniqueEntityId)).Select(d => d.Id);
                        //找出所有的装入件
                        var loadListThree = (from cc in loadQuery
                                             join mm in workIdsThreeAll on cc.WorkStepEntityId equals mm
                                             select cc).AsNoTracking();
                        if (proTemp.Count > 0)
                        {
                            foreach (var item in proTemp)
                            {
                                int ProcessDosage = 0;
                                string matingName = "";
                                var workIdsThree = _workStepEntityRepository.GetAll().Where(d => d.ProcessesEntityId == item.Id).Select(d => d.Id);
                                if (workIdsThree.Count() > 0)
                                {
                                    var loadData = from loadPieceList in _loadPieceRepository.GetAll().Where(d => workIdsThree.Contains(d.WorkStepEntityId)).OrderBy(d => d.Sort)
                                                   join materialsList in _materialsContentRepository.GetAll() on loadPieceList.MaterialsContentId equals materialsList.Id
                                                   select loadPieceList;

                                    if (loadData.Count() > 0)
                                    {
                                        ProcessDosage = (from loadPieceList in loadQuery.Where(d => workIdsThree.Contains(d.WorkStepEntityId)) select loadPieceList).Sum(t => t.Number);
                                        var collectContent = _collectBomManagerRepository.FirstOrDefaultAsync(t => t.XJName == coreGradeNode.NodeName && t.XJDrawingNo == coreGradeNode.NodeDrawingNo && t.ModuleId == bopDesc.ModelId && t.SendTimeId == bopDesc.LotNoId).Result;
                                        if (collectContent != null)
                                        {
                                            var collectBomMaterialsContentDtos = _collectBomAppServiceRepository.GetCollectBomMaterialsContent(collectContent.XJId);
                                            if (collectBomMaterialsContentDtos == null)
                                            {
                                                result.Message = "该芯级下没有汇总数据，请检查";
                                                result.code = 1;
                                                return result;
                                            }
                                            var storeStockContentFirst = _storeStockContentRepsitory.GetAll().Where(t => t.BomAnalysecontentId == collectContent.Id && t.ProductCodeId.Contains(collectContent.ModuleId.ToString()) && t.SendTimeId.Contains(collectContent.SendTimeId.ToString()));
                                            var storeStockId = storeStockContentFirst.Select(t => t.StoreStockId).Distinct();
                                            //查询芯级下的所有预配套数据
                                            var queryCollectMatingNumberAlys = from queryloadContent in collectBomMaterialsContentDtos.AsQueryable()
                                                                               join queryStoreStocks in _storeStockRepository.GetAll() on
                                                                             queryloadContent.MerialsId equals queryStoreStocks.MaterialsContentId
                                                                               join queryStoreStockContents in stockContent.Where(t => t.Batch == "01" && t.IsPreSuit == IsSuits.是 && t.BomAnalysecontentId != null && t.BomAnalysecontentId == collectContent.Id) on queryStoreStocks.Id equals queryStoreStockContents.StoreStockId
                                                                               select queryStoreStockContents;
                                            //查询芯级下的所有配套数据
                                            var queryCollectMatingNumber = from queryloadContent in loadData
                                                                           join queryStoreStocks in _storeStockRepository.GetAll() on
                                                                             queryloadContent.MaterialsContentId equals queryStoreStocks.MaterialsContentId
                                                                           join queryStoreStockContents in stockContent.Where(t => t.Batch == "01" && t.IsPreSuit == IsSuits.是 && t.BomAnalysecontentId != null && t.IsSuit == IsSuits.是 && t.LoadDetailsId != null && t.WorkStepId != null && t.BomAnalysecontentId == collectContent.Id) on queryStoreStocks.Id equals queryStoreStockContents.StoreStockId
                                                                           where queryStoreStockContents.LoadDetailsId == queryloadContent.Id
                                                                           select queryStoreStockContents;

                                            //汇总可预配套数量 汇总配套数据
                                            var queryyptListCount = from queryloadContent in loadListThree
                                                                    join queryStoreStocks in _storeStockRepository.GetAll() on
                                                                      queryloadContent.MaterialsContentId equals queryStoreStocks.MaterialsContentId
                                                                    join queryStoreStockContents in stockContent.Where(t => t.Batch == "01" && t.IsPreSuit == IsSuits.是 && t.BomAnalysecontentId != null && t.IsSuit == IsSuits.是 && t.LoadDetailsId != null && t.WorkStepId != null && t.BomAnalysecontentId == collectContent.Id) on queryStoreStocks.Id equals queryStoreStockContents.StoreStockId
                                                                    where queryStoreStockContents.LoadDetailsId == queryloadContent.Id
                                                                    select new StoreStockContent
                                                                    {
                                                                        ProductDrawingNo = queryStoreStockContents.ProductDrawingNo,
                                                                        Module = queryStoreStockContents.Module,
                                                                        WorkStepId = queryloadContent.WorkStepEntityId,
                                                                        StoreStockId = queryStoreStocks.Id,
                                                                        MaterialsContentId = queryStoreStocks.MaterialsContentId,
                                                                        BomAnalysecontentId = queryStoreStockContents.BomAnalysecontentId
                                                                    };
                                            //if (queryyptList != null && queryyptList .Count()>0&& queryCollectMatingNumber != null&& queryCollectMatingNumber.Count()>0)
                                            //{
                                            var storeStorkContent = (from tempLoad in loadData
                                                                     join tempStroeStocks in _storeStockRepository.GetAll()
                                                                     on tempLoad.MaterialsContentId equals tempStroeStocks.MaterialsContentId
                                                                     join storestocks in storeStockId on tempStroeStocks.Id equals storestocks
                                                                     //join tempStockContent in _storeStockContentRepsitory.GetAll() on tempStroeStocks.Id equals tempStockContent.StoreStockId
                                                                     join tempMaterial in _materialsContentRepository.GetAll() on tempStroeStocks.MaterialsContentId equals tempMaterial.Id
                                                                         into tm
                                                                     from tmi in tm.DefaultIfEmpty()
                                                                         //join queryyptlistCount in queryyptListCount on storestocks == null?Guid.Empty: storestocks equals queryyptlistCount ==null?Guid.Empty: queryyptlistCount.StoreStockId
                                                                         //into qlc from ql in qlc.DefaultIfEmpty()
                                                                     join tempNode in _materialsNodeRepository.GetAll() on tmi.MaterialsNodeId equals tempNode.Id
                                                                     into tn
                                                                     from tpn in tn.DefaultIfEmpty()
                                                                         //join tempSearch in queryyptList on tempLoad ==null? Guid.Empty: tempLoad.MaterialsContentId equals (tempSearch == null ? Guid.Empty : tempSearch.MaterialsContentId) into ca
                                                                         //from ts in ca.DefaultIfEmpty()
                                                                     join tempSysCode in _sysCodeRepository.GetAll() on tmi.UnitId equals tempSysCode.Id
                                                                        into sc
                                                                     from cc in sc.DefaultIfEmpty()
                                                                         //where (ts == null ? Guid.Empty : ts.LoadDetailsId) == tempLoad.Id
                                                                     orderby tempLoad.Sort
                                                                     //where (ts==null?Guid.Empty:ts.LoadDetailsId) == tempLoad.Id
                                                                     orderby tempLoad.Sort
                                                                     select new MatingDetaillist
                                                                     {
                                                                         data = new MatingEditChildData()
                                                                         {
                                                                             Code = tmi == null ? "" : tmi.MaterialNo,//物料代码
                                                                             Module = tmi == null ? "" : tmi.Model,//规格
                                                                             StoreStockId = tempStroeStocks.Id,
                                                                             CollectId = collectContent.Id,
                                                                             Number = tempLoad.Number,//工艺用量
                                                                             PreSuiteStock = queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectContent.Id),
                                                                             DeliveryStock = queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id && t.OutboundState == OutboundStates.出库),
                                                                             UsableMatingQuantityStock = queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectContent.Id) - queryyptListCount.Count(t => t.StoreStockId == tempStroeStocks.Id),
                                                                             //queryyptListCount.FirstOrDefault(t => t.StoreStockId == storestocks).Quantity)
                                                                             WorkStepId = tempLoad.WorkStepEntityId,
                                                                             SuiteStock = queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id),
                                                                             LoadDetailsId = tempLoad.Id,
                                                                             // 
                                                                             //tempStroeStocks.PreSuiteStock - tempStroeStocks.SuiteStock != 0 说明存在可用的配套数量 有可能存在负数
                                                                             //- ts.Quantity
                                                                             //&& tempLoad.Number - (tempStroeStocks.PreSuiteStock - tempStroeStocks.SuiteStock)>0
                                                                             StarvingStock = tempLoad.Number - (queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectContent.Id) - queryyptListCount.Count(t => t.StoreStockId == tempStroeStocks.Id)) - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) <= 0 ? 0 :
                                                                 tempLoad.Number - (queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectContent.Id) - queryyptListCount.Count(t => t.StoreStockId == tempStroeStocks.Id)) - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id),
                                                                             MatingStatus = tempLoad.Number - (queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id) - queryyptListCount.Count(t => t.StoreStockId == tempStroeStocks.Id)) - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) > 0 ? (int)AssortStatus.缺料 :
                                                                 tempLoad.Number - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) <= (queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectContent.Id)) - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) && queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) != 0 && tempLoad.Number > queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) ? (int)AssortStatus.配套中 : tempLoad.Number == queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) ? (int)AssortStatus.已配套 : (int)AssortStatus.未配套,
                                                                             MatingStatusName = tempLoad.Number - (queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id) - queryyptListCount.Count(t => t.StoreStockId == tempStroeStocks.Id)) - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) > 0 ? AssortStatus.缺料.ToString() :
                                                                 tempLoad.Number - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) <= (queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectContent.Id)) - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) && queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) != 0 && tempLoad.Number > queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) ? AssortStatus.配套中.ToString() : tempLoad.Number == queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) ? AssortStatus.已配套.ToString() : "未配套",
                                                                             CompletePeople = tempStroeStocks.CompletePeople,
                                                                             CompleteTime = tempStroeStocks.CompleteTime.ToString(),
                                                                             PreSuiteName = tempStroeStocks.PreSuiteName,
                                                                             PreSuiteTime = tempStroeStocks.PreSuiteTime.ToString(),
                                                                             ProductNumber = tempStroeStocks.ProductNumber
                                                                         }
                                                                     }).ToList();

                                            if (storeStorkContent != null && storeStorkContent.Count > 0)
                                            {
                                                var tempStore = (from tqy in loadData
                                                                 join queryLoadData in storeStorkContent on tqy.Id equals queryLoadData.data.LoadDetailsId
                                                                 into qld
                                                                 from qlda in qld.DefaultIfEmpty()
                                                                 select new MatingDetaillist
                                                                 {
                                                                     data = new MatingEditChildData
                                                                     {
                                                                         HJXH = qlda == null ? "" : qlda.data.HJXH,
                                                                         HJFC = qlda == null ? "" : qlda.data.HJFC,
                                                                         CollectId = qlda == null ? Guid.Empty : qlda.data.CollectId,
                                                                         Code = qlda == null ? tqy.Code : qlda.data.Code,
                                                                         CompletePeople = qlda == null ? "" : qlda.data.CompletePeople,
                                                                         CompleteTime = qlda == null ? "" : qlda.data.CompleteTime,
                                                                         DeliveryStock = qlda == null ? 0 : qlda.data.DeliveryStock,
                                                                         Id = qlda == null ? Guid.Empty : qlda.data.Id,
                                                                         IsSuit = qlda == null ? 0 : qlda.data.IsSuit,
                                                                         JoinText = qlda == null ? "" : qlda.data.JoinText,
                                                                         LevelName = qlda == null ? "" : qlda.data.LevelName,
                                                                         LevelNum = qlda == null ? 0 : qlda.data.LevelNum,
                                                                         LoadDetailsId = qlda == null ? tqy.Id : qlda.data.LoadDetailsId,
                                                                         MaterialsNodeName = qlda == null ? "" : qlda.data.MaterialsNodeName,
                                                                         MaterialsTypeName = qlda == null ? "" : qlda.data.MaterialsTypeName,
                                                                         MatingStatus = qlda == null ? (int)AssortStatus.缺料 : qlda.data.MatingStatus,
                                                                         MatingStatusName = qlda == null ? "缺料" : qlda.data.MatingStatusName,
                                                                         Module = qlda == null ? "" : qlda.data.Module,
                                                                         Name = tqy.Name,
                                                                         NodeLevel = qlda == null ? 10 : qlda.data.NodeLevel,
                                                                         Number = qlda == null ? tqy.Number : qlda.data.Number,
                                                                         PreSuiteName = qlda == null ? "" : qlda.data.PreSuiteName,
                                                                         PreSuiteStock = qlda == null ? 0 : qlda.data.PreSuiteStock,
                                                                         PreSuiteTime = qlda == null ? "" : qlda.data.PreSuiteTime,
                                                                         ProductNumber = qlda == null ? "" : qlda.data.ProductNumber,
                                                                         StarvingStock = qlda == null ? 0 : qlda.data.StarvingStock,
                                                                         StoreStockId = qlda == null ? Guid.Empty : qlda.data.StoreStockId,
                                                                         SuiteStock = qlda == null ? 0 : qlda.data.SuiteStock,
                                                                         Unit = qlda == null ? "" : qlda.data.Unit,
                                                                         UsableMatingQuantityStock = qlda == null ? 0 : qlda.data.UsableMatingQuantityStock,
                                                                         UsableQuantityStock = qlda == null ? 0 : qlda.data.UsableQuantityStock,
                                                                         WorkStepId = qlda == null ? tqy.WorkStepEntityId : qlda.data.WorkStepId,
                                                                         XJName = qlda == null ? "" : qlda.data.XJName,
                                                                     }
                                                                 }
                                                               ).ToList();
                                                foreach (MatingDetaillist matingDetaillist in tempStore)
                                                {
                                                    var bs = storeStorkContent.Where(t => t.data.MatingStatus == (int)AssortStatus.缺料);

                                                    if (bs != null && bs.Count() > 0)
                                                    {
                                                        matingName = AssortStatus.缺料.ToString();
                                                        break;
                                                    }
                                                    if (matingDetaillist.data.MatingStatus == (int)AssortStatus.缺料)
                                                    {
                                                        matingName = AssortStatus.缺料.ToString();
                                                        break;
                                                    }
                                                    else if (matingDetaillist.data.MatingStatus == (int)AssortStatus.配套中)
                                                    {
                                                        matingName = AssortStatus.配套中.ToString();
                                                        break;
                                                    }
                                                    else if (matingDetaillist.data.MatingStatus == (int)AssortStatus.已配套)
                                                    {
                                                        matingName = AssortStatus.已配套.ToString();
                                                        break;
                                                    }
                                                    else if (matingDetaillist.data.MatingStatus == (int)AssortStatus.未配套)
                                                    {
                                                        matingName = AssortStatus.未配套.ToString();
                                                        break;
                                                    }
                                                }
                                            }
                                            //计算提取芯级预配套数量、可用预配套数量 进行去重
                                            var contentRequest = storeStorkContent.ToList().Select(x => new { x.data.Code, x.data.Module, x.data.PreSuiteStock, x.data.UsableMatingQuantityStock }).Distinct();

                                            //计算配套数量缺料数量的汇总
                                            var groupByBtContent = (from guoupAll in (from groupContent in (from queryStoreStock in storeStorkContent select queryStoreStock)
                                                                                      group groupContent by new { groupContent.data.CollectId, groupContent.data.Module, groupContent.data.Code } into gc
                                                                                      select new
                                                                                      {
                                                                                          PreSuiteStockAll = contentRequest.Where(t => t.Module == gc.Key.Module && t.Code == gc.Key.Code).Sum(t => t.PreSuiteStock),
                                                                                          UsableMatingQuantityStockAll = contentRequest.Where(t => t.Module == gc.Key.Module && t.Code == gc.Key.Code).Sum(t => t.UsableMatingQuantityStock),//可用预配套数量
                                                                                          SuiteStockAll = gc.Sum(t => t.data.SuiteStock),
                                                                                          StarvingStockAll = gc.Sum(t => t.data.StarvingStock),
                                                                                          DeliveryStockAll = gc.Sum(t => t.data.DeliveryStock),
                                                                                          CollectId = gc.Key.CollectId
                                                                                      })
                                                                    group guoupAll by new { guoupAll.CollectId } into all
                                                                    select new
                                                                    {
                                                                        PreSuiteStockAll = all.Sum(t => t.PreSuiteStockAll),
                                                                        UsableMatingQuantityStockAll = all.Sum(t => t.UsableMatingQuantityStockAll),
                                                                        SuiteStockAll = all.Sum(t => t.SuiteStockAll),
                                                                        StarvingStockAll = all.Sum(t => t.StarvingStockAll),
                                                                        DeliveryStockAll = all.Sum(t => t.DeliveryStockAll)
                                                                    }).FirstOrDefault();

                                            MatingTreeGridDto mating = new MatingTreeGridDto();
                                            MatingEditParentData matingpd = new MatingEditParentData();
                                            matingpd.Code = "工序" + item.ProcessesSequence.ToString() + " "; ;
                                            matingpd.Name = item.ProcessesName;
                                            matingpd.Id = item.Id;
                                            matingpd.JoinText = "工序";
                                            matingpd.NodeLevel = 8;
                                            Level++;
                                            matingpd.LevelName = Level.ToString();
                                            if (groupByBtContent != null)
                                            {
                                                if (ProcessDosage > (groupByBtContent.UsableMatingQuantityStockAll + groupByBtContent.SuiteStockAll))
                                                {
                                                    matingpd.MatingStatusName = AssortStatus.缺料.ToString();
                                                    matingpd.StarvingStock = ProcessDosage - (groupByBtContent.UsableMatingQuantityStockAll + groupByBtContent.SuiteStockAll);
                                                    matingpd.PreSuiteStock = groupByBtContent.PreSuiteStockAll;
                                                }
                                                else
                                                {
                                                    matingpd.MatingStatusName = matingName;
                                                    matingpd.StarvingStock = groupByBtContent.StarvingStockAll;
                                                }
                                                matingpd.UsableMatingQuantityStock = groupByBtContent.UsableMatingQuantityStockAll;
                                                matingpd.SuiteStock = groupByBtContent.SuiteStockAll;
                                                matingpd.DeliveryStock = groupByBtContent.DeliveryStockAll;
                                            }

                                            matingpd.Number = ProcessDosage;
                                            mating.data = matingpd;
                                            outputs.Add(mating);
                                        }
                                    }
                                }
                            }
                        }
                        break;
                    case 4:
                        var proceTemp = _processesEntityRepository.Get(input.NodeId);
                        int numberCount = 0;
                        string xjcode = "";
                        string xjName = "";

                        //获取工序下的所有工步
                        var workTemp = _workStepEntityRepository.GetAll().Where(d => d.ProcessesEntityId == input.NodeId).OrderBy(d => d.WorkStepNumber).ToList();
                        if (workTemp.Count > 0)
                        {
                            var coreNodeInputList = (from temp in _bopNodeRepository.GetAll().Where(d => d.TechniqueEntityId == proceTemp.TechniqueEntityId)
                                                     select new
                                                     {
                                                         ParentCode = temp.ParentCode.Substring(0, 3),
                                                         BopDescId = temp.BopDescId
                                                     }).FirstOrDefault();
                            //型号发次
                            var moduleLotNo = _bopDescRepository.FirstOrDefaultAsync(t => t.Id == coreNodeInputList.BopDescId).Result;

                            if (coreNodeInputList != null)
                            {
                                var coreNode = _bopNodeRepository.GetAll().Where(d => d.NodeCode == coreNodeInputList.ParentCode && d.ParentId == coreNodeInputList.BopDescId).FirstOrDefault();
                                if (coreNode != null)
                                {
                                    xjcode = coreNode.NodeDrawingNo;
                                    xjName = coreNode.NodeName;
                                }
                                //找出所有的规程  （找到所有规程 能找到所有的装入件、contains）
                                var techQuery = _bopNodeRepository.GetAll().Where(d => d.NodeCode.StartsWith(coreNode.NodeCode) && d.BopDescId == coreNode.BopDescId).Where(d => d.TechniqueEntityId != null).Select(d => d.TechniqueEntityId).ToList();
                                var workIds = _workStepEntityRepository.GetAll().Where(d => techQuery.Contains(d.TechniqueEntityId)).Select(d => d.Id);
                                //找出所有的装入件
                                var loadList = (from cc in loadQuery
                                                join mm in workIds on cc.WorkStepEntityId equals mm
                                                select cc).AsNoTracking();
                                //找出所有工步的id
                                var workIdList = workTemp.Select(d => d.Id).ToList();
                                //根据工步获取装入件明细 所有工步下的装入件明细

                                //var lo = _loadPieceRepository.GetAll().Where(d => workIdList.Contains(d.WorkStepEntityId)).OrderBy(d => d.Sort);

                                var loadData = (from loadPieceList in _loadPieceRepository.GetAll().Where(d => workIdList.Contains(d.WorkStepEntityId)).OrderBy(d => d.Sort)
                                                join materialsList in _materialsContentRepository.GetAll() on loadPieceList.MaterialsContentId equals materialsList.Id
                                                into mll
                                                from ml in mll.DefaultIfEmpty()
                                                select loadPieceList).AsNoTracking();

                                ////获取可用预配套数量
                                //var storeStocksContent = from queryMaterislsContent in _materialsContentRepository.GetAll()
                                //                         join queryStoreStocks in _storeStockRepsitory.GetAll() on
                                //                         queryMaterislsContent.Id equals queryStoreStocks.MaterialsContentId
                                //                         join queryStoreStockContents in _storeStockContentRepsitory.GetAll().
                                //                         Where(t => t.Batch == "01" && t.IsPreSuit == IsSuits.是&&t.BomAnalysecontentId != null&&t.LoadDetailsId==null&&t.IsSuit  == IsSuits.否) on queryStoreStocks.Id equals queryStoreStockContents.StoreStockId
                                //                         join queryloadData in loadData on queryMaterislsContent.Id equals queryloadData.MaterialsContentId
                                //                         select queryStoreStockContents;
                                //找出预配套的数量
                                List<MatingDetaillist> tempQuery = new List<MatingDetaillist>();

                                if (loadData.Count() > 0)
                                {
                                    var collectContent = _collectBomManagerRepository.FirstOrDefaultAsync(t => t.XJName == xjName && t.XJDrawingNo == xjcode && t.ModuleId == moduleLotNo.ModelId && t.SendTimeId == moduleLotNo.LotNoId).Result;
                                    if (collectContent != null)
                                    {
                                        //throw new UserFriendlyException("该节点下没有对应的芯级");

                                        var collectBomMaterialsContentDtos = _collectBomAppServiceRepository.GetCollectBomMaterialsContent(collectContent.XJId);
                                        if (collectBomMaterialsContentDtos == null || collectContent == null)
                                        {
                                            result.Message = "该芯级下没有汇总数据，请检查";
                                            result.code = 1;
                                            return result;
                                        }
                                        var storeStockContentFirst = _storeStockContentRepsitory.GetAll().Where(t => t.BomAnalysecontentId == collectContent.Id && t.ProductCodeId.Contains(collectContent.ModuleId.ToString()) && t.SendTimeId.Contains(collectContent.SendTimeId.ToString()));
                                        var storeStockId = storeStockContentFirst.Select(t => t.StoreStockId).Distinct();
                                        //查询芯级下的所有预配套数据
                                        var queryCollectMatingNumberAlys = from queryloadContent in collectBomMaterialsContentDtos.AsQueryable()
                                                                           join queryStoreStocks in _storeStockRepository.GetAll() on
                                                                         queryloadContent.MerialsId equals queryStoreStocks.MaterialsContentId
                                                                           join queryStoreStockContents in stockContent.Where(t => t.Batch == "01" && t.IsPreSuit == IsSuits.是 && t.BomAnalysecontentId != null && t.BomAnalysecontentId == collectContent.Id) on queryStoreStocks.Id equals queryStoreStockContents.StoreStockId
                                                                           select queryStoreStockContents;
                                        //查询芯级下的所有配套数据
                                        var queryCollectMatingNumber = from queryloadContent in loadData
                                                                       join queryStoreStocks in _storeStockRepository.GetAll() on
                                                                         queryloadContent.MaterialsContentId equals queryStoreStocks.MaterialsContentId
                                                                       join queryStoreStockContents in stockContent.Where(t => t.Batch == "01" && t.IsPreSuit == IsSuits.是 && t.BomAnalysecontentId != null && t.IsSuit == IsSuits.是 && t.LoadDetailsId != null && t.WorkStepId != null && t.BomAnalysecontentId == collectContent.Id) on queryStoreStocks.Id equals queryStoreStockContents.StoreStockId
                                                                       where queryStoreStockContents.LoadDetailsId == queryloadContent.Id
                                                                       select queryStoreStockContents;

                                        //汇总可预配套数量 汇总配套数据
                                        var queryyptListCount = from queryloadContent in loadList
                                                                join queryStoreStocks in _storeStockRepository.GetAll() on
                                                                  queryloadContent.MaterialsContentId equals queryStoreStocks.MaterialsContentId
                                                                join queryStoreStockContents in stockContent.Where(t => t.Batch == "01" && t.IsPreSuit == IsSuits.是 && t.BomAnalysecontentId != null && t.IsSuit == IsSuits.是 && t.LoadDetailsId != null && t.WorkStepId != null && t.BomAnalysecontentId == collectContent.Id) on queryStoreStocks.Id equals queryStoreStockContents.StoreStockId
                                                                where queryStoreStockContents.LoadDetailsId == queryloadContent.Id
                                                                select new StoreStockContent
                                                                {
                                                                    ProductDrawingNo = queryStoreStockContents.ProductDrawingNo,
                                                                    Module = queryStoreStockContents.Module,
                                                                    WorkStepId = queryloadContent.WorkStepEntityId,
                                                                    StoreStockId = queryStoreStocks.Id,
                                                                    MaterialsContentId = queryStoreStocks.MaterialsContentId,
                                                                    BomAnalysecontentId = queryStoreStockContents.BomAnalysecontentId
                                                                };
                                        try
                                        {
                                            tempQuery = (from tempLoad in loadData
                                                         join tempStroeStocks in _storeStockRepository.GetAll()
                                                         on tempLoad.MaterialsContentId equals tempStroeStocks.MaterialsContentId
                                                         join storestocks in storeStockId on tempStroeStocks.Id equals storestocks
                                                         //join tempStockContent in _storeStockContentRepsitory.GetAll() on tempStroeStocks.Id equals tempStockContent.StoreStockId
                                                         join tempMaterial in _materialsContentRepository.GetAll() on tempStroeStocks.MaterialsContentId equals tempMaterial.Id
                                                             into tm
                                                         from tmi in tm.DefaultIfEmpty()
                                                             //join queryyptlistCount in queryyptListCount on storestocks == null?Guid.Empty: storestocks equals queryyptlistCount ==null?Guid.Empty: queryyptlistCount.StoreStockId
                                                             //into qlc from ql in qlc.DefaultIfEmpty()
                                                         join tempNode in _materialsNodeRepository.GetAll() on tmi.MaterialsNodeId equals tempNode.Id
                                                         into tn
                                                         from tpn in tn.DefaultIfEmpty()
                                                             //join tempSearch in queryyptList on tempLoad ==null? Guid.Empty: tempLoad.MaterialsContentId equals (tempSearch == null ? Guid.Empty : tempSearch.MaterialsContentId) into ca
                                                             //from ts in ca.DefaultIfEmpty()
                                                         join tempSysCode in _sysCodeRepository.GetAll() on tmi.UnitId equals tempSysCode.Id
                                                            into sc
                                                         from cc in sc.DefaultIfEmpty()
                                                             //where (ts == null ? Guid.Empty : ts.LoadDetailsId) == tempLoad.Id
                                                         orderby tempLoad.Sort
                                                         select new MatingDetaillist
                                                         {
                                                             data = new MatingEditChildData()
                                                             {
                                                                 HJFC = collectContent.SendTimeId.ToString(),
                                                                 HJXH = collectContent.ModuleId.ToString(),
                                                                 StoreStockId = tempStroeStocks.Id,
                                                                 Id = Guid.NewGuid(),
                                                                 CollectId = collectContent.Id,
                                                                 LoadDetailsId = tempLoad.Id,
                                                                 MaterialsTypeName = tpn == null ? "" : tpn.MaterialTypeName,//物料类别s
                                                                 Name = tempLoad.Name,// 物料名称
                                                                 Code = tmi == null ? "" : tempLoad.Code,//物料代码
                                                                 Module = tmi == null ? "" : tmi.Model,//规格
                                                                 Unit = cc == null ? "" : cc.Description,//单位
                                                                 Number = tempLoad.Number,//工艺用量
                                                                 MaterialsNodeName = tpn == null ? "" : tpn.MaterialTypeName,
                                                                 PreSuiteStock = queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectContent.Id),
                                                                 UsableMatingQuantityStock = queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectContent.Id) - queryyptListCount.Count(t => t.StoreStockId == tempStroeStocks.Id),
                                                                 //queryyptListCount.FirstOrDefault(t => t.StoreStockId == storestocks).Quantity)
                                                                 WorkStepId = tempLoad.WorkStepEntityId,
                                                                 SuiteStock = queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id),
                                                                 DeliveryStock = queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id && t.OutboundState == OutboundStates.出库),
                                                                 StarvingStock = tempLoad.Number - (queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectContent.Id) - queryyptListCount.Count(t => t.StoreStockId == tempStroeStocks.Id)) - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) <= 0 ? 0 :
                                                                 tempLoad.Number - (queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectContent.Id) - queryyptListCount.Count(t => t.StoreStockId == tempStroeStocks.Id)) - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id),
                                                                 MatingStatus = tempLoad.Number - (queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id) - queryyptListCount.Count(t => t.StoreStockId == tempStroeStocks.Id)) - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) > 0 ? (int)AssortStatus.缺料 :
                                                                 tempLoad.Number - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) <= (queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectContent.Id)) - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) && queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) != 0 && tempLoad.Number > queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) ? (int)AssortStatus.配套中 : tempLoad.Number == queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) ? (int)AssortStatus.已配套 : (int)AssortStatus.未配套,
                                                                 MatingStatusName = tempLoad.Number - (queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id) - queryyptListCount.Count(t => t.StoreStockId == tempStroeStocks.Id)) - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) > 0 ? AssortStatus.缺料.ToString() :
                                                                 tempLoad.Number - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) <= (queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectContent.Id)) - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) && queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) != 0 && tempLoad.Number > queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) ? AssortStatus.配套中.ToString() : tempLoad.Number == queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) ? AssortStatus.已配套.ToString() : "未配套",
                                                                 CompletePeople = queryCollectMatingNumber.FirstOrDefault(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id && t.IsSuit == IsSuits.是 && t.LoadDetailsId != null) != null ? queryCollectMatingNumber.FirstOrDefault(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id && t.IsSuit == IsSuits.是 && t.LoadDetailsId != null).CompletePeople : "",
                                                                 CompleteTime = queryCollectMatingNumber.FirstOrDefault(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id && t.IsSuit == IsSuits.是 && t.LoadDetailsId != null) != null ? queryCollectMatingNumber.FirstOrDefault(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id && t.IsSuit == IsSuits.是 && t.LoadDetailsId != null).CompleteTime.ToString() : "",
                                                                 PreSuiteName = queryCollectMatingNumberAlys.FirstOrDefault(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectContent.Id) != null ? queryCollectMatingNumberAlys.FirstOrDefault(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectContent.Id).PreSuiteName : "",
                                                                 PreSuiteTime = queryCollectMatingNumberAlys.FirstOrDefault(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectContent.Id) != null ? queryCollectMatingNumberAlys.FirstOrDefault(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectContent.Id).PreSuiteTime.ToString() : "",
                                                                 ProductNumber = tempStroeStocks.ProductNumber,
                                                                 NodeLevel = 10
                                                             }
                                                         }).AsNoTracking().ToList();
                                            //循环工步id
                                            for (int i = 0; i < workIdList.Count; i++)
                                            {
                                                //
                                                var work = workTemp.Where(d => d.Id == workIdList[i]).First();
                                                MatingTreeGridDto mating = new MatingTreeGridDto();
                                                MatingEditParentData matingpd = new MatingEditParentData();
                                                //工步id
                                                matingpd.Id = work.Id;
                                                matingpd.Code = "工步" + proceTemp.ProcessesSequence.ToString() + "-" + work.WorkStepNumber.ToString();
                                                matingpd.Name = work.WorkStepNumber.ToString();
                                                matingpd.JoinText = "工步";
                                                matingpd.LevelName = (i + 1).ToString();
                                                mating.data = matingpd;
                                                mating.data.NodeLevel = 9;
                                                //matingpd.MatingStatusName = "缺料";
                                                //根据每个工步id 过滤出装入件明细
                                                var query = loadData.Where(d => d.WorkStepEntityId == workIdList[i]).ToList();


                                                if (query.Count > 0)
                                                {
                                                    //找出对应的配套数量
                                                    var tempStore = (from tqy in query
                                                                     join materContent in _materialsContentRepository.GetAll() on tqy.MaterialsContentId equals materContent.Id
                                               into mc
                                                                     from msd in mc.DefaultIfEmpty()
                                                                     join queryLoadData in tempQuery on tqy.Id equals tempQuery.Count == 0 ? Guid.Empty : queryLoadData.data.LoadDetailsId
                                                              into qld
                                                                     from qlda in qld.DefaultIfEmpty()
                                                                     join materNode in _materialsNodeRepository.GetAll() on msd == null ? Guid.Empty : msd.MaterialsNodeId equals materNode.Id into mn
                                                                     from mnd in mn.DefaultIfEmpty()
                                                                     join sysCode in _sysCodeRepository.GetAll() on msd == null ? Guid.Empty : msd.UnitId equals sysCode.Id
                                                                     into syc
                                                                     from sy in syc.DefaultIfEmpty()

                                                                     select new MatingDetaillist
                                                                     {
                                                                         data = new MatingEditChildData
                                                                         {
                                                                             HJXH = qlda == null ? "" : qlda.data.HJXH,
                                                                             HJFC = qlda == null ? "" : qlda.data.HJFC,
                                                                             CollectId = qlda == null ? Guid.Empty : qlda.data.CollectId,
                                                                             Code = qlda == null ? _storeFunctionsAppService.GetStandardDrawingNoDispose(tqy.Code).ProductDrawingNo : _storeFunctionsAppService.GetStandardDrawingNoDispose(qlda.data.Code).ProductDrawingNo,
                                                                             CompletePeople = qlda == null ? "" : qlda.data.CompletePeople,
                                                                             CompleteTime = qlda == null ? "" : qlda.data.CompleteTime,
                                                                             DeliveryStock = qlda == null ? 0 : qlda.data.DeliveryStock,
                                                                             Id = qlda == null ? Guid.Empty : qlda.data.Id,
                                                                             IsSuit = qlda == null ? 0 : qlda.data.IsSuit,
                                                                             JoinText = qlda == null ? "" : qlda.data.JoinText,
                                                                             LevelName = qlda == null ? "" : qlda.data.LevelName,
                                                                             LevelNum = qlda == null ? 0 : qlda.data.LevelNum,
                                                                             LoadDetailsId = qlda == null ? tqy.Id : qlda.data.LoadDetailsId,
                                                                             MaterialsNodeName = qlda == null ? mnd == null ? "" : mnd.MaterialTypeName : qlda.data.MaterialsNodeName,
                                                                             MaterialsTypeName = qlda == null ? mnd == null ? "" : mnd.MaterialTypeName : qlda.data.MaterialsTypeName,
                                                                             MatingStatus = qlda == null ? (int)AssortStatus.缺料 : qlda.data.MatingStatus,
                                                                             MatingStatusName = qlda == null ? "缺料" : qlda.data.MatingStatusName,
                                                                             Module = qlda == null ? _storeFunctionsAppService.GetStandardDrawingNoDispose(tqy.Code).Module : _storeFunctionsAppService.GetStandardDrawingNoDispose(qlda.data.Code).Module,
                                                                             Name = tqy.Name,
                                                                             NodeLevel = qlda == null ? 10 : qlda.data.NodeLevel,
                                                                             Number = qlda == null ? tqy.Number : qlda.data.Number,
                                                                             PreSuiteName = qlda == null ? "" : qlda.data.PreSuiteName,
                                                                             PreSuiteStock = qlda == null ? 0 : qlda.data.PreSuiteStock,
                                                                             PreSuiteTime = qlda == null ? "" : qlda.data.PreSuiteTime,
                                                                             ProductNumber = qlda == null ? "" : qlda.data.ProductNumber,
                                                                             StarvingStock = qlda == null ? 0 : qlda.data.StarvingStock,
                                                                             StoreStockId = qlda == null ? Guid.Empty : qlda.data.StoreStockId,
                                                                             SuiteStock = qlda == null ? 0 : qlda.data.SuiteStock,
                                                                             Unit = qlda == null ? sy == null ? "" : sy.Description : qlda.data.Unit,
                                                                             UsableMatingQuantityStock = qlda == null ? 0 : qlda.data.UsableMatingQuantityStock,
                                                                             UsableQuantityStock = qlda == null ? 0 : qlda.data.UsableQuantityStock,
                                                                             WorkStepId = qlda == null ? tqy.WorkStepEntityId : qlda.data.WorkStepId,
                                                                             XJName = qlda == null ? "" : qlda.data.XJName,
                                                                             YearsNo = qlda == null ? _storeFunctionsAppService.GetStandardDrawingNoDispose(tqy.Code).YearNo : _storeFunctionsAppService.GetStandardDrawingNoDispose(qlda.data.Code).YearNo
                                                                         }
                                                                     }
                                                                    ).ToList();
                                                    if (tempStore.Count > 0)
                                                    {
                                                        for (int j = 0; j < tempStore.Count(); j++)
                                                        {
                                                            if (tempStore[j] != null)
                                                            {
                                                                tempStore[j].data.LevelName = matingpd.LevelName + "." + (j + 1).ToString();
                                                            }
                                                        }
                                                    }

                                                    mating.children = tempStore;
                                                }
                                                outputs.Add(mating);
                                            }
                                        }
                                        catch (Exception ex)
                                        {

                                            throw;
                                        }

                                    }
                                }

                            }
                        }
                        break;
                    case 5:
                        var workStep = _workStepEntityRepository.FirstOrDefault(d => d.Id == input.NodeId);

                        string xjcodeFive = "";
                        string xjNameFive = "";
                        if (workStep != null)
                        {

                            var workStepInput = (from temp in _bopNodeRepository.GetAll().Where(d => d.TechniqueEntityId == workStep.TechniqueEntityId)
                                                 select new
                                                 {
                                                     ParentCode = temp.ParentCode.Substring(0, 3),
                                                     BopDescId = temp.BopDescId
                                                 }).FirstOrDefault();
                            if (workStepInput != null)
                            {
                                var coreNode = _bopNodeRepository.GetAll().Where(d => d.NodeCode == workStepInput.ParentCode && d.BopDescId == workStepInput.BopDescId).FirstOrDefault();
                                if (coreNode != null)
                                {
                                    xjNameFive = coreNode.NodeName;
                                    xjcodeFive = coreNode.NodeDrawingNo;
                                }
                                var techQuery = _bopNodeRepository.GetAll().Where(d => d.NodeCode.StartsWith(coreNode.NodeCode) && d.BopDescId == coreNode.BopDescId).Where(d => d.TechniqueEntityId != null).Select(d => d.TechniqueEntityId).ToList();
                                if (techQuery.Count > 0)
                                {
                                    var workIds = _workStepEntityRepository.GetAll().Where(d => techQuery.Contains(d.TechniqueEntityId)).Select(d => d.Id);

                                    //获取或有的装入件
                                    var loadList = from cc in loadQuery
                                                   join mm in workIds on cc.WorkStepEntityId equals mm
                                                   select cc;


                                    var process = _processesEntityRepository.Get(workStep.ProcessesEntityId);
                                    //获取
                                    var loadData = from loadPieceList in _loadPieceRepository.GetAll().Where(d => d.WorkStepEntityId == workStep.Id).OrderBy(d => d.Sort)
                                                   join materialsList in _materialsContentRepository.GetAll() on loadPieceList.MaterialsContentId equals materialsList.Id
                                                   select loadPieceList;


                                    //获取该工步下所有的装入件
                                    var loadDataNo = from loadPieceList in _loadPieceRepository.GetAll().Where(d => d.WorkStepEntityId == workStep.Id).OrderBy(d => d.Sort)
                                                     select loadPieceList;
                                    if (loadData.Count() > 0)
                                    {
                                        var moduleLotNo = _bopDescRepository.FirstOrDefaultAsync(t => t.Id == workStepInput.BopDescId).Result;
                                        var collectContent = _collectBomManagerRepository.FirstOrDefaultAsync(t => t.XJName == xjNameFive && t.XJDrawingNo == xjcodeFive && t.ModuleId == moduleLotNo.ModelId && t.SendTimeId == moduleLotNo.LotNoId).Result;
                                        if (collectContent != null)
                                        {
                                            // throw new UserFriendlyException("该节点下没有对应的芯级");

                                            var collectBomMaterialsContentDtos = _collectBomAppServiceRepository.GetCollectBomMaterialsContent(collectContent.XJId);
                                            if (collectBomMaterialsContentDtos == null)
                                            {
                                                result.Message = "该芯级下没有汇总数据，请检查";
                                                result.code = 1;
                                                return result;
                                            }
                                            var storeStockContentFirst = _storeStockContentRepsitory.GetAll().Where(t => t.BomAnalysecontentId == collectContent.Id && t.ProductCodeId.Contains(collectContent.ModuleId.ToString()) && t.SendTimeId.Contains(collectContent.SendTimeId.ToString()));
                                            var storeStockId = storeStockContentFirst.Select(t => t.StoreStockId).Distinct();

                                            //查询芯级下的所有配套数据
                                            var queryCollectMatingNumberAlys = from queryloadContent in collectBomMaterialsContentDtos.AsQueryable()
                                                                               join queryStoreStocks in _storeStockRepository.GetAll() on
                                                                             queryloadContent.MerialsId equals queryStoreStocks.MaterialsContentId
                                                                               join queryStoreStockContents in stockContent.Where(t => t.Batch == "01" && t.IsPreSuit == IsSuits.是 && t.BomAnalysecontentId != null && t.BomAnalysecontentId == collectContent.Id) on queryStoreStocks.Id equals queryStoreStockContents.StoreStockId
                                                                               select queryStoreStockContents;
                                            //查询芯级下的所有配套数据
                                            var queryCollectMatingNumber = from queryloadContent in loadData
                                                                           join queryStoreStocks in _storeStockRepository.GetAll() on
                                                                             queryloadContent.MaterialsContentId equals queryStoreStocks.MaterialsContentId
                                                                           join queryStoreStockContents in stockContent.Where(t => t.Batch == "01" && t.IsPreSuit == IsSuits.是 && t.BomAnalysecontentId != null && t.IsSuit == IsSuits.是 && t.LoadDetailsId != null && t.WorkStepId != null && t.BomAnalysecontentId == collectContent.Id) on queryStoreStocks.Id equals queryStoreStockContents.StoreStockId
                                                                           where queryStoreStockContents.LoadDetailsId == queryloadContent.Id
                                                                           select queryStoreStockContents;
                                            //汇总可预配套数量 汇总配套数据
                                            var queryyptListCount = from queryloadContent in loadList
                                                                    join queryStoreStocks in _storeStockRepository.GetAll() on
                                                                      queryloadContent.MaterialsContentId equals queryStoreStocks.MaterialsContentId
                                                                    join queryStoreStockContents in stockContent.Where(t => t.Batch == "01" && t.IsPreSuit == IsSuits.是 && t.BomAnalysecontentId != null && t.IsSuit == IsSuits.是 && t.LoadDetailsId != null && t.WorkStepId != null && t.BomAnalysecontentId == collectContent.Id) on queryStoreStocks.Id equals queryStoreStockContents.StoreStockId
                                                                    where queryStoreStockContents.LoadDetailsId == queryloadContent.Id
                                                                    select new StoreStockContent
                                                                    {
                                                                        ProductDrawingNo = queryStoreStockContents.ProductDrawingNo,
                                                                        Module = queryStoreStockContents.Module,
                                                                        WorkStepId = queryloadContent.WorkStepEntityId,
                                                                        StoreStockId = queryStoreStocks.Id,
                                                                        MaterialsContentId = queryStoreStocks.MaterialsContentId,
                                                                        BomAnalysecontentId = queryStoreStockContents.BomAnalysecontentId
                                                                    };
                                            var tempQuery = from tempLoad in loadData
                                                            join tempStroeStocks in _storeStockRepository.GetAll()
                                                            on tempLoad.MaterialsContentId equals tempStroeStocks.MaterialsContentId
                                                            join storestocks in storeStockId on tempStroeStocks.Id equals storestocks
                                                            //join tempStockContent in _storeStockContentRepsitory.GetAll() on tempStroeStocks.Id equals tempStockContent.StoreStockId
                                                            join tempMaterial in _materialsContentRepository.GetAll() on tempStroeStocks.MaterialsContentId equals tempMaterial.Id
                                                                into tm
                                                            from tmi in tm.DefaultIfEmpty()
                                                            join tempNode in _materialsNodeRepository.GetAll() on tmi.MaterialsNodeId equals tempNode.Id
                                                            into tn
                                                            from tpn in tn.DefaultIfEmpty()
                                                                //join tempSearch in queryyptList on tempLoad ==null? Guid.Empty: tempLoad.MaterialsContentId equals (tempSearch == null ? Guid.Empty : tempSearch.MaterialsContentId) into ca
                                                                //from ts in ca.DefaultIfEmpty()
                                                            join tempSysCode in _sysCodeRepository.GetAll() on tmi.UnitId equals tempSysCode.Id
                                                               into sc
                                                            from cc in sc.DefaultIfEmpty()
                                                                //where (ts==null?Guid.Empty:ts.LoadDetailsId) == tempLoad.Id
                                                            orderby tempLoad.Sort
                                                            select new MatingTreeGridDto
                                                            {
                                                                data = new MatingEditParentData()
                                                                {
                                                                    HJFC = collectContent.SendTimeId.ToString(),
                                                                    HJXH = collectContent.ModuleId.ToString(),
                                                                    StoreStockId = tempStroeStocks.Id,
                                                                    Id = Guid.NewGuid(),
                                                                    CollectId = collectContent.Id,
                                                                    LoadDetailsId = tempLoad.Id,
                                                                    MaterialsTypeName = tpn == null ? "" : tpn.MaterialTypeName,//物料类别s
                                                                    Name = tempLoad.Name,// 物料名称
                                                                    Code = tmi == null ? "" : tempLoad.Code,//物料代码
                                                                    Module = tmi == null ? "" : tmi.Model,//规格
                                                                    Unit = cc == null ? "" : cc.Description,//单位
                                                                    Number = tempLoad.Number,//工艺用量
                                                                    MaterialsNodeName = tpn == null ? "" : tpn.MaterialTypeName,
                                                                    PreSuiteStock = queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectContent.Id),
                                                                    UsableMatingQuantityStock = queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectContent.Id) - queryyptListCount.Count(t => t.StoreStockId == tempStroeStocks.Id),
                                                                    //queryyptListCount.FirstOrDefault(t => t.StoreStockId == storestocks).Quantity)
                                                                    WorkStepId = tempLoad.WorkStepEntityId,
                                                                    SuiteStock = queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id),
                                                                    // 
                                                                    //tempStroeStocks.PreSuiteStock - tempStroeStocks.SuiteStock != 0 说明存在可用的配套数量 有可能存在负数
                                                                    //- ts.Quantity
                                                                    //&& tempLoad.Number - (tempStroeStocks.PreSuiteStock - tempStroeStocks.SuiteStock)>0
                                                                    StarvingStock = tempLoad.Number - (queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectContent.Id) - queryyptListCount.Count(t => t.StoreStockId == tempStroeStocks.Id)) - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) <= 0 ? 0 :
                                                             tempLoad.Number - (queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectContent.Id) - queryyptListCount.Count(t => t.StoreStockId == tempStroeStocks.Id)) - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id),
                                                                    //queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectContent.Id) - queryyptListCount.Count(t => t.StoreStockId == tempStroeStocks.Id) > queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) ? 0 :
                                                                    //     tempLoad.Number - (queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectContent.Id) - queryyptListCount.Count(t => t.StoreStockId == tempStroeStocks.Id)) - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id),
                                                                    DeliveryStock = queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.OutboundState == OutboundStates.出库),
                                                                    MatingStatusName = tempLoad.Number - (queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id) - queryyptListCount.Count(t => t.StoreStockId == tempStroeStocks.Id)) - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) > 0 ? AssortStatus.缺料.ToString() :
                                                                         tempLoad.Number - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) <= (queryCollectMatingNumberAlys.Count(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectContent.Id)) - queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) && queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) != 0 && tempLoad.Number > queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) ? AssortStatus.配套中.ToString() : tempLoad.Number == queryCollectMatingNumber.Count(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id) ? AssortStatus.已配套.ToString() : "未配套",
                                                                    CompletePeople = queryCollectMatingNumber.FirstOrDefault(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id && t.IsSuit == IsSuits.是 && t.LoadDetailsId != null) != null ? queryCollectMatingNumber.FirstOrDefault(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id && t.IsSuit == IsSuits.是 && t.LoadDetailsId != null).CompletePeople : "",
                                                                    CompleteTime = queryCollectMatingNumber.FirstOrDefault(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id && t.IsSuit == IsSuits.是 && t.LoadDetailsId != null) != null ? queryCollectMatingNumber.FirstOrDefault(t => t.StoreStockId == tempStroeStocks.Id && t.WorkStepId == tempLoad.WorkStepEntityId && t.LoadDetailsId == tempLoad.Id && t.IsSuit == IsSuits.是 && t.LoadDetailsId != null).CompleteTime.ToString() : "",
                                                                    PreSuiteName = queryCollectMatingNumberAlys.FirstOrDefault(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectContent.Id) != null ? queryCollectMatingNumberAlys.FirstOrDefault(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectContent.Id).PreSuiteName : "",
                                                                    PreSuiteTime = queryCollectMatingNumberAlys.FirstOrDefault(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectContent.Id) != null ? queryCollectMatingNumberAlys.FirstOrDefault(t => t.StoreStockId == tempStroeStocks.Id && t.BomAnalysecontentId == collectContent.Id).PreSuiteTime.ToString() : "",
                                                                    ProductNumber = tempStroeStocks.ProductNumber,
                                                                    NodeLevel = 10
                                                                }
                                                            };
                                            try
                                            {
                                                if (tempQuery.Count() > 0)
                                                {
                                                    var tempStore = (from tqy in loadDataNo.ToList()
                                                                     join queryLoadData in tempQuery.ToList()
                                                                     on tqy.Id equals queryLoadData.data.LoadDetailsId
                                                                     into qld
                                                                     from qlda in qld.DefaultIfEmpty()
                                                                     select new MatingTreeGridDto
                                                                     {
                                                                         data = new MatingEditParentData
                                                                         {
                                                                             HJXH = qlda == null ? "" : qlda.data.HJXH,
                                                                             HJFC = qlda == null ? "" : qlda.data.HJFC,
                                                                             CollectId = qlda == null ? Guid.Empty : qlda.data.CollectId,
                                                                             Code = qlda == null ? _storeFunctionsAppService.GetStandardDrawingNoDispose(tqy.Code).ProductDrawingNo : _storeFunctionsAppService.GetStandardDrawingNoDispose(qlda.data.Code).ProductDrawingNo,
                                                                             CompletePeople = qlda == null ? "" : qlda.data.CompletePeople,
                                                                             CompleteTime = qlda == null ? "" : qlda.data.CompleteTime,
                                                                             DeliveryStock = qlda == null ? 0 : qlda.data.DeliveryStock,
                                                                             Id = qlda == null ? Guid.Empty : qlda.data.Id,
                                                                             IsSuit = qlda == null ? 0 : qlda.data.IsSuit,
                                                                             JoinText = qlda == null ? "" : qlda.data.JoinText,
                                                                             LevelName = qlda == null ? "" : qlda.data.LevelName,
                                                                             LevelNum = qlda == null ? 0 : qlda.data.LevelNum,
                                                                             LoadDetailsId = qlda == null ? tqy.Id : qlda.data.LoadDetailsId,
                                                                             MaterialsNodeName = qlda == null ? "" : qlda.data.MaterialsNodeName,
                                                                             MaterialsTypeName = qlda == null ? "" : qlda.data.MaterialsTypeName,
                                                                             MatingStatus = qlda == null ? (int)AssortStatus.缺料 : qlda.data.MatingStatus,
                                                                             MatingStatusName = qlda == null ? "缺料" : qlda.data.MatingStatusName,
                                                                             Module = qlda == null ? _storeFunctionsAppService.GetStandardDrawingNoDispose(tqy.Code).Module : _storeFunctionsAppService.GetStandardDrawingNoDispose(qlda.data.Code).Module,
                                                                             Name = tqy.Name,
                                                                             NodeLevel = qlda == null ? 10 : qlda.data.NodeLevel,
                                                                             Number = qlda == null ? tqy.Number : qlda.data.Number,
                                                                             PreSuiteName = qlda == null ? "" : qlda.data.PreSuiteName,
                                                                             PreSuiteStock = qlda == null ? 0 : qlda.data.PreSuiteStock,
                                                                             PreSuiteTime = qlda == null ? "" : qlda.data.PreSuiteTime,
                                                                             ProductNumber = qlda == null ? "" : qlda.data.ProductNumber,
                                                                             StarvingStock = qlda == null ? 0 : qlda.data.StarvingStock,
                                                                             StoreStockId = qlda == null ? Guid.Empty : qlda.data.StoreStockId,
                                                                             SuiteStock = qlda == null ? 0 : qlda.data.SuiteStock,
                                                                             Unit = qlda == null ? "" : qlda.data.Unit,
                                                                             UsableMatingQuantityStock = qlda == null ? 0 : qlda.data.UsableMatingQuantityStock,
                                                                             UsableQuantityStock = qlda == null ? 0 : qlda.data.UsableQuantityStock,
                                                                             WorkStepId = qlda == null ? tqy.WorkStepEntityId : qlda.data.WorkStepId,
                                                                             XJName = qlda == null ? "" : qlda.data.XJName,
                                                                             YearsNo = qlda == null ? _storeFunctionsAppService.GetStandardDrawingNoDispose(tqy.Code).YearNo : _storeFunctionsAppService.GetStandardDrawingNoDispose(qlda.data.Code).YearNo
                                                                         }
                                                                     }
                                             ).ToList();
                                                    outputs = tempStore;
                                                }
                                                else
                                                {
                                                    var tempStore = (from tqy in loadDataNo.ToList()
                                                                     join materContent in _materialsContentRepository.GetAll() on tqy.MaterialsContentId equals materContent.Id
                                                 into mc
                                                                     from msd in mc.DefaultIfEmpty()
                                                                     join materNode in _materialsNodeRepository.GetAll() on msd.MaterialsNodeId equals materNode.Id into mn
                                                                     from mnd in mn.DefaultIfEmpty()
                                                                     join sysCode in _sysCodeRepository.GetAll() on msd.UnitId equals sysCode.Id
                                                                     into syc
                                                                     from sy in syc.DefaultIfEmpty()
                                                                     select new MatingTreeGridDto
                                                                     {
                                                                         data = new MatingEditParentData
                                                                         {
                                                                             Code = _storeFunctionsAppService.GetStandardDrawingNoDispose(tqy.Code).ProductDrawingNo,
                                                                             LoadDetailsId = tqy.Id,
                                                                             Name = tqy.Name,
                                                                             Number = tqy.Number,
                                                                             MaterialsNodeName = mnd.MaterialTypeName,
                                                                             Module = _storeFunctionsAppService.GetStandardDrawingNoDispose(tqy.Code).Module,
                                                                             Unit = sy.Description,
                                                                             WorkStepId = tqy.WorkStepEntityId,
                                                                             YearsNo = _storeFunctionsAppService.GetStandardDrawingNoDispose(tqy.Code).YearNo,
                                                                             NodeLevel = 10
                                                                         }
                                                                     }
                                                   ).ToList();
                                                    outputs = tempStore;
                                                }
                                            }
                                            catch (Exception ex)
                                            {

                                                throw;
                                            }

                                        }
                                    }
                                }
                            }
                        }
                        break;
                    default:
                        break;
                }
                result.MatingTreeGridList = outputs;
                return result;
            }
            else
            {
                throw new Exception("传入数据有误!");
            }
        }
    }
}
